<template>
    <div>
        <el-row :gutter="20">
            <el-col :span="8">
                <el-card shadow="hover" class="mgb20" style="height:220px;">
                    <div class="user-info">
                        <img :src="imgsrc" class="user-avator" alt />
                        <div class="user-info-cont">
                            <div class="user-info-name">{{name}}</div>
                            <div>{{role}}</div>
                        </div>
                    </div>
                    <div class="user-info-list">
                        <span>{{name}}，{{getTimeState()}}</span>
                    </div>
                </el-card>
                <el-card shadow="hover" style="height:484px;">
                    <div slot="header" class="clearfix">
                        <span>版本统计</span>
                    </div>
                    <div ref="myChart" style="height: 300px"></div>
                </el-card>
            </el-col>

            <el-col :span="16">
                <el-row :gutter="20" class="mgb20">
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{padding: '0px'}">
                            <div class="grid-content grid-con-1">
                                <i class="el-icon-lx-calendar grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">1234</div>
                                    <div>我的项目</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{padding: '0px'}">
                            <div class="grid-content grid-con-2">
                                <i class="el-icon-lx-read grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">321</div>
                                    <div>项目版本</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{padding: '0px'}">
                            <div class="grid-content grid-con-3">
                                <i class="el-icon-lx-time grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">5000</div>
                                    <div>使用记录</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                </el-row>

                <el-card shadow="hover" style="height:603px;">
                    <div slot="header" class="clearfix">
                        <span>最近项目列表</span>
                    </div>
                    <el-table :show-header="true" :data="proList" style="width:100%;">
                        <el-table-column type="index" width="100"></el-table-column>
                        <el-table-column v-if="false" property="projectId" label="项目编号"></el-table-column>
                        <el-table-column property="projectNm" label="项目名称" width="400"></el-table-column>
                        <el-table-column property="proPeople" label="负责人" width="300"></el-table-column>
                        <el-table-column property="projectDate" label="研发日期" width="200"></el-table-column>
                    </el-table>
                </el-card>
            </el-col>
        </el-row>
    </div>
</template>

<script>
    import {doIndex} from '../../api/index';

    export default {
        name: 'dashboard',
        data() {
            return {
                name: localStorage.getItem('ms_userAcct'),
                proList: [],
                legendData: [],
                seriesData: []
            };
        },
        computed: {
            role() {
                return this.name === 'admin' ? '超级管理员' : '普通用户';
            },
            imgsrc() {
                let imgtp = localStorage.getItem('ms_imgTp');
                let imgdata = localStorage.getItem('ms_imgData');
                if (imgtp != 'null' && imgdata != 'null' && imgtp != "" && imgdata != ""){
                    return 'data:image/'+ imgtp +';base64,' + imgdata;
                } else {
                    return require("@/assets/img/user_img.gif");
                }
            }
        },
        mounted() {
            this.doIndex();
        },
        methods: {
            doIndex(){
                doIndex().then(res =>{
                    if (res.success){
                        console.log("调用响应结果是:{}", res)
                        this.proList = res.data.proList;
                        this.legendData = res.data.legendData;
                        this.seriesData = res.data.seriesData;
                        this.myNewEcharts();
                    }
                });
            },
            getTimeState(){
                // 获取当前时间
                let timeNow = new Date();
                // 获取当前小时
                let hours = timeNow.getHours();
                // 设置默认文字
                let state= ``;
                // 判断当前时间段
                if (hours >= 0 && hours <= 10) {
                    state = `早上好!`;
                } else if (hours > 10 && hours <= 14) {
                    state= `中午好!`;
                } else if (hours > 14 && hours <= 18) {
                    state= `下午好!`;
                } else if (hours > 18 && hours <= 24) {
                    state= `晚上好!`;
                }
                return state;
            },
            myNewEcharts(){
                let option = {
                    title: {
                        text: '',
                        subtext: '',
                        left: 'center'
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: '{a} <br/>{b} : {c} ({d}%)'
                    },
                    legend: {
                        type: 'scroll',
                        orient: 'vertical',
                        right: 5,
                        top: 5,
                        bottom: 5,
                        data: this.legendData
                    },
                    series: [
                        {
                            name: '设备名',
                            type: 'pie',
                            radius: '70%', //调节饼图大小
                            center: ['40%', '50%'], //调节饼图左右位置
                            data: this.seriesData,
                            emphasis: {
                                itemStyle: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                };
                let myChart = this.$echarts.init(this.$refs.myChart);
                myChart.showLoading();
                myChart.hideLoading();
                myChart.setOption(option);
            }
        }
    };
</script>


<style scoped>
.el-row {
    margin-bottom: 20px;
}

.grid-content {
    display: flex;
    align-items: center;
    height: 100px;
}

.grid-cont-right {
    flex: 1;
    text-align: center;
    font-size: 14px;
    color: #999;
}

.grid-num {
    font-size: 30px;
    font-weight: bold;
}

.grid-con-icon {
    font-size: 50px;
    width: 100px;
    height: 100px;
    text-align: center;
    line-height: 100px;
    color: #fff;
}

.grid-con-1 .grid-con-icon {
    background: rgb(81, 182, 204);
}

.grid-con-1 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-2 .grid-con-icon {
    background: rgb(159, 207, 132);
}

.grid-con-2 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-3 .grid-con-icon {
    background: rgb(234, 151, 72);
}

.grid-con-3 .grid-num {
    color: rgb(145, 242, 234);
}

.user-info {
    display: flex;
    align-items: center;
    padding-bottom: 20px;
    border-bottom: 2px solid #ccc;
    margin-bottom: 20px;
}

.user-avator {
    width: 120px;
    height: 120px;
    border-radius: 50%;
}

.user-info-cont {
    padding-left: 50px;
    flex: 1;
    font-size: 14px;
    color: #999;
}

.user-info-cont div:first-child {
    font-size: 30px;
    color: #222;
}

.user-info-list {
    font-size: 14px;
    color: #999;
    line-height: 25px;
}

.user-info-list span {
    margin-left: 70px;
}

.mgb20 {
    margin-bottom: 20px;
}

.todo-item {
    font-size: 14px;
}

.todo-item-del {
    text-decoration: line-through;
    color: #999;
}

.schart {
    width: 100%;
    height: 300px;
}
</style>
